<?php

/**
 * Paciente form base class.
 *
 * @package    form
 * @subpackage paciente
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 8508 2008-04-17 17:39:15Z fabien $
 */
class BasePacienteForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'               => new sfWidgetFormInputHidden(),
      'parentesco_id'    => new sfWidgetFormDoctrineChoice(array('model' => 'Parentesco', 'add_empty' => true)),
      'estado_civil_id'  => new sfWidgetFormDoctrineChoice(array('model' => 'EstadoCivil', 'add_empty' => true)),
      'tipo_sangre_id'   => new sfWidgetFormDoctrineChoice(array('model' => 'TipoSangre', 'add_empty' => true)),
      'familia_id'       => new sfWidgetFormDoctrineChoice(array('model' => 'Familia', 'add_empty' => false)),
      'distrito_id'      => new sfWidgetFormDoctrineChoice(array('model' => 'Distrito', 'add_empty' => false)),
      'dni'              => new sfWidgetFormInput(),
      'nombre'           => new sfWidgetFormInput(),
      'apellido_paterno' => new sfWidgetFormInput(),
      'apellido_materno' => new sfWidgetFormInput(),
      'fecha_nacimiento' => new sfWidgetFormDate(),
      'sexo'             => new sfWidgetFormInput(),
      'imagen'           => new sfWidgetFormInput(),
      'direccion'        => new sfWidgetFormInput(),
      'tipo_paciente'    => new sfWidgetFormInput(),
      'descripcion'      => new sfWidgetFormTextarea(),
      'estado'           => new sfWidgetFormInput(),
      'slug'             => new sfWidgetFormInput(),
      'created_at'       => new sfWidgetFormDateTime(),
      'updated_at'       => new sfWidgetFormDateTime(),
      'alergias_list'    => new sfWidgetFormDoctrineChoiceMany(array('model' => 'Alergia')),
    ));

    $this->setValidators(array(
      'id'               => new sfValidatorDoctrineChoice(array('model' => 'Paciente', 'column' => 'id', 'required' => false)),
      'parentesco_id'    => new sfValidatorDoctrineChoice(array('model' => 'Parentesco', 'required' => false)),
      'estado_civil_id'  => new sfValidatorDoctrineChoice(array('model' => 'EstadoCivil', 'required' => false)),
      'tipo_sangre_id'   => new sfValidatorDoctrineChoice(array('model' => 'TipoSangre', 'required' => false)),
      'familia_id'       => new sfValidatorDoctrineChoice(array('model' => 'Familia')),
      'distrito_id'      => new sfValidatorDoctrineChoice(array('model' => 'Distrito')),
      'dni'              => new sfValidatorString(array('max_length' => 8)),
      'nombre'           => new sfValidatorString(array('max_length' => 100)),
      'apellido_paterno' => new sfValidatorString(array('max_length' => 100)),
      'apellido_materno' => new sfValidatorString(array('max_length' => 100)),
      'fecha_nacimiento' => new sfValidatorDate(array('required' => false)),
      'sexo'             => new sfValidatorString(array('max_length' => 1, 'required' => false)),
      'imagen'           => new sfValidatorString(array('max_length' => 105)),
      'direccion'        => new sfValidatorString(array('max_length' => 255)),
      'tipo_paciente'    => new sfValidatorString(array('max_length' => 1, 'required' => false)),
      'descripcion'      => new sfValidatorString(array('max_length' => 2147483647, 'required' => false)),
      'estado'           => new sfValidatorString(array('max_length' => 1)),
      'slug'             => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'created_at'       => new sfValidatorDateTime(array('required' => false)),
      'updated_at'       => new sfValidatorDateTime(array('required' => false)),
      'alergias_list'    => new sfValidatorDoctrineChoiceMany(array('model' => 'Alergia', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorDoctrineUnique(array('model' => 'Paciente', 'column' => array('slug')))
    );

    $this->widgetSchema->setNameFormat('paciente[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Paciente';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['alergias_list']))
    {
      $this->setDefault('alergias_list', $this->object->Alergias->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveAlergiasList($con);
  }

  public function saveAlergiasList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['alergias_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Alergias->getPrimaryKeys();
    $values = $this->getValue('alergias_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Alergias', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Alergias', array_values($link));
    }
  }

}
